<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="playground.css">
  </head>
  <body ng-controller="searchController">
    <div class="container">
      <form action="#">
        <div class="autocomplete-wrapper">
          <div class="form-group">
            <div class="col-sm-4">
              <h4>Simple auto-complete</h4>
              <div class="input-group">
                <input id="contacts" name="contacts" class="form-control" type="text" ng-model="q" autocomplete aa-datasets="getDatasets()" aa-options="{ appendTo: 'body', debug: true, hint: false }" />
                <span class="input-group-addon">Go</span>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>

    <script src="../node_modules/jquery/dist/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.min.js"></script>
    <script src="../dist/autocomplete.angular.js"></script>
    <script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.angular.min.js"></script>
    <script>
    angular.module('myApp', ['algoliasearch', 'algolia.autocomplete']).controller('searchController', ['$scope', 'algolia', function($scope, algolia) {
      $scope.q = '';
      var client = algolia.Client('latency', '6be0576ff61c053d5f9a3225e2a90f76');
      var index = client.initIndex('contacts');

      $scope.getDatasets = function() {
        return {
          source: algolia.sources.hits(index, { hitsPerPage: 5 }),
          templates: {
            suggestion: function(suggestion) {
              return suggestion._highlightResult.name.value;
            }
          }
        };
      };

      $scope.$watch('q', function(v) {
        console.log(v);
      });

      $scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
        console.log(suggestion, dataset);
      });
    }]);
    </script>
  </body>
</html>
